#include "../include/threadPool.h"
int Register(MYSQL *mysql,const char *userName, const char *userPwd){
    char id[21] = "$6$";
    char random[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
    int random_size = sizeof(random)-1;
    srand(time(NULL));
    char salt[17] = {0};
    int num;
    for(int i = 0;i < 16;i++){
        num = rand()% random_size;
        salt[i] = random[num];
    }
    strcat(id,salt);
    strcat(id,"$");
    struct crypt_data userData;
    bzero(&userData,sizeof(struct crypt_data));
    crypt_r(userPwd,id,&userData);// 根据明文密码和盐值得到密文密码
    //printf("%s\n",userData.output);
    int ret = addUser(mysql,userName,userData.output,id);
    if(ret == -1){
        printf("增加用户表项失败!\n");
        return -1;
    }
    /* char path[10] = "/"; */
    fileMeta_t meta;
    bzero(&meta,sizeof(fileMeta_t));
    strcpy(meta.fileName,userName);
    strcpy(meta.userName,userName);
    meta.parentId = -1;
    meta.fileType = 'd';
    strcpy(meta.filePath,"/");
    /* strcpy(meta.filePath,path); */
    ret = insertDataToFileTable(mysql,&meta);
    if(ret == -1){
        printf("注册失败!\n");
        return -1;
    }
    printf("注册成功!\n");
    return 0;
}

int logIn(MYSQL *mysql,const char *userName, const char *userPwd){
    char salt[30] = {0};
    checkSalt(mysql,userName,salt);
    struct crypt_data userData;
    bzero(&userData,sizeof(struct crypt_data));
    crypt_r(userPwd,salt,&userData);// 根据明文密码和盐值得到密文密码
    int ret = checkPassword(mysql,userName,userData.output);
    return ret == 0?0:-1;
}


//int main(){
//    MYSQL *mysql = mysql_init(NULL);
//    MYSQL *ret = mysql_real_connect(mysql,"localhost","root","123","cpp66th",0,NULL,0);
//    if(ret == NULL){
//        fprintf(stderr,"%s\n", mysql_error(mysql));
//        exit(-1);
//    }
//    //Register(mysql,"hyl","123456");
//    logon(mysql,"hyl","123456");
//    mysql_close(mysql);
//    return 0;
//}
